package controllers

import (
	"fmt"
	"math"
	"myerp/models"
	"myerp/src"
	"strconv"
	"time"
)

//客户管理
type ClientsController struct {
	CommonController
}

//添加
func (c *ClientsController) Add() {
	if c.Ctx.Request.Method == "POST" {
		reData := make(map[string]interface{})
		reData["status"] = 1

		var md models.Clients
		if err := c.ParseForm(&md); err != nil {
			fmt.Println(err)
		}
		fmt.Println(md)
		//md.Name = c.GetString("name")
		//md.Pid, _ = c.GetInt("pid", 0)
		md.Ctime = int(time.Now().Unix())
		_, err := models.AddClients(&md)
		if err != nil {
			c.Data["satus"] = 0
			c.Data["message"] = "添加失败"
			c.Data["uri"] = "/clients/add"
		} else {
			c.Data["message"] = "添加成功"
			c.Data["uri"] = "/clients/list"
			c.Data["st"] = 1
		}
		c.TplName = "error.html"
		return
	}
	//获取以及分类
	l, _ := models.GetAeraChind(0)
	c.Data["ActionName"] = "添加分类"
	c.Data["ol"] = l

	c.Layout = "base.html"
	c.TplName = "clients/add.html"
}

//列表
func (c *ClientsController) List() {
	//pid, _ := c.GetInt("pid", 0)
	//l, _ := models.GetAllClientsByPid(pid)
	var query map[string]string
	var fields []string
	var sortby []string
	var order []string
	var offset int64
	var limit int64 = 10
	var page int64
	count, err := models.GetClientsCount(query)

	page, _ = c.GetInt64("page", 0)
	if page <= 1 {
		offset = 0
	} else {
		offset = (page - 1) * limit
	}
	if page*limit > count {
		if limit > count {
			offset = 0
		} else {
			s := math.Floor(float64(count)/float64(limit)) * float64(limit)
			offset = int64(s)
		}
	}

	l, err := models.GetAllClients(query, fields, sortby, order, offset, limit)
	fmt.Println(err)
	var pages src.Page = src.NewPage(int(page), 10, int(count), "/clients/list")
	//fmt.Println(pages.Show)
	c.Data["page"] = pages.Show()
	c.Data["ActionName"] = "分类列表"
	c.Data["List"] = l
	c.Layout = "base.html"
	c.TplName = "clients/list.html"

}

//删除分类
func (c *ClientsController) Delete() {
	Id := c.GetString("id")
	resId, _ := strconv.Atoi(Id)
	err := models.DeleteClients(resId)
	reData := make(map[string]interface{})
	reData["status"] = 0
	reData["msg"] = "删除失败！"
	if err == nil {
		reData["status"] = 1
		reData["msg"] = "删除成功"
	}
	c.Data["json"] = reData
	c.ServeJSON()
}

//js获取列表
func (c *ClientsController) Ajaxlist() {
	reData := make(map[string]interface{})
	reData["status"] = 0
	pid, err := c.GetInt("pid")
	if err != nil || pid < 1 {
		reData["tl"] = ""
		c.Data["json"] = reData
		c.ServeJSON()
		return
	}
	l, _ := models.GetAllSortsByPid(pid)
	reData["tl"] = l
	reData["status"] = 1
	c.Data["json"] = reData
	c.ServeJSON()
}

//编辑
func (c *ClientsController) Edite() {
	var id int
	if c.Ctx.Request.Method == "POST" {
		reData := make(map[string]interface{})
		reData["status"] = 0
		var md models.Clients
		if err := c.ParseForm(&md); err != nil {
			//handle error
		}
		err := models.UpdateClientsById(&md)
		if err != nil {
			c.Data["st"] = 0
			c.Data["message"] = "修改失败"
			c.Data["uri"] = "/clients/edite?id=" + c.GetString("Id")
		} else {
			c.Data["message"] = "修改成功"
			c.Data["uri"] = "/clients/list"
			c.Data["st"] = 1
		}
		c.TplName = "error.html"
		return
	}

	id, err := c.GetInt("id")
	if err != nil {
		c.Data["message"] = "修改的内容不存在"
		c.Data["uri"] = "/clients/list"
		c.Data["st"] = 0
		c.TplName = "error.html"
		return
	}
	sd, err := models.GetClientsById(id)
	if err != nil {
		c.Data["message"] = "修改的内容不存在"
		c.Data["uri"] = "/clients/list"
		c.Data["st"] = 0
		c.TplName = "error.html"
		return
	}
	//获取一级城市
	one, _ := models.GetAeraChind(0)
	c.Data["one"] = one
	//获取二级城市
	two, _ := models.GetAeraChind(sd.Province)
	c.Data["two"] = two
	//获取三级城市
	if sd.City > 0 {
		three, _ := models.GetAeraChind(sd.City)
		c.Data["three"] = three
	}

	c.Data["Sdata"] = sd
	c.Layout = "base.html"
	c.TplName = "clients/edite.html"

}
